package jms;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


public final class MessageSpec {
	private final Object message;
	private final MessageCreator creator;
	private final MessageDispatcher dispatcher;
	
	private  MessageSpec(Object message, MessageCreator creator,
			MessageDispatcher dispatcher) {
		super();
		this.message = message;
		this.creator = creator;
		this.dispatcher = dispatcher;
	}
	
	public static final class MessageSpecBuilder {
		private Object message;
		private MessageCreator creator;
		private MessageDispatcher dispatcher;
		
		public MessageSpec.MessageSpecBuilder setCreator(MessageCreator creator) {
			this.creator = creator;
			return this;
		}
		
		public MessageSpec.MessageSpecBuilder setDispatcher(MessageDispatcher dispatcher) {
			this.dispatcher = dispatcher;
			return this;
		}
		
		public MessageSpec.MessageSpecBuilder setMessage(Object message) {
			this.message = message;
			return this;
		}
		
		public MessageSpec build() {
			return new MessageSpec(message, creator, dispatcher);
		}
	}
	
	public MessageDispatcher getDispatcher() {
		return dispatcher;
	}
	
	public Message buildMessage(Session session) throws JMSException {
		return creator.create(session, this.message);
	}
}